home *** CD-ROM | disk | FTP | other *** search
/ SGI Freeware 1999 August / SGI Freeware 1999 August.iso / dist / fw_ghostscript.idb / usr / freeware / lib / ghostscript / 3.33 / ps2epsi.ps.z / ps2epsi.ps
Encoding:
Text File  |  1998-05-21  |  5.3 KB  |  184 lines

  1. %    Copyright (C) 1990, 1991, 1993, 1994 Aladdin Enterprises.  All rights reserved.
  2. % This file is part of GNU Ghostscript.
  3. % GNU Ghostscript is distributed in the hope that it will be useful, but
  4. % WITHOUT ANY WARRANTY.  No author or distributor accepts responsibility to
  5. % anyone for the consequences of using it or for whether it serves any
  6. % particular purpose or works at all, unless he says so in writing.  Refer
  7. % to the GNU Ghostscript General Public License for full details.
  8.  
  9. % Convert an arbitrary PostScript file to an EPSI file.
  10. %
  11. %    NOTE: this file was contributed by a user: please contact
  12. %    George Cameron <george@bio-medical-physics.aberdeen.ac.uk>
  13. %    if you have questions.
  14. %
  15.  
  16. % Initialize, and redefine copypage and showpage.
  17. /ps2edict 25 dict def
  18. ps2edict begin
  19.  
  20.                 % The main procedure
  21.   /ps2epsi
  22.    {                % Open the file
  23.      outfile (w) file /epsifile exch def
  24.                     % Get the device parameters
  25.      currentdevice getdeviceprops .dicttomark
  26.      /HWSize get aload pop
  27.        /devheight exch def
  28.        /devwidth exch def
  29.      matrix defaultmatrix
  30.        /devmatrix exch def
  31.                 % Make a corresponding memory device
  32.      devmatrix devwidth devheight <ff 00>
  33.      makeimagedevice
  34.      /arraydevice exch def
  35.      arraydevice setdevice    % (does an erasepage)
  36.      /rowwidth devwidth 7 add 8 idiv def
  37.      /row rowwidth string def
  38.                 % Replace the definition of showpage
  39.      userdict /showpage { ps2edict begin epsipage end } bind put
  40.    } def
  41.  
  42.  /margintest
  43.  {
  44.       { dup arraydevice exch row copyscanlines pop
  45.     -1 0 1 rowwidth 1 sub
  46.     { dup row exch get 0 ne { exit }{ pop } ifelse
  47.     } for
  48.     dup -1 ne { exch pop } if
  49.     -1 ne { exit } if pop
  50.       } for
  51.  } bind def
  52.  
  53.   /epsipage
  54.    {
  55.      /loopcount devheight 1 sub def
  56.  
  57.      % Find top margin
  58.      -1 0 1 loopcount margintest
  59.      dup -1 eq { (blank page!!\n) print 0 .quit }{ exch pop } ifelse 
  60.      /tm exch def
  61.  
  62.      % Find bottom margin
  63.      loopcount -1 0 margintest
  64.      /bm exch def
  65.      
  66.      % Initialise limit variables
  67.      /loopcount rowwidth 1 sub def
  68.      /lm loopcount def /lmb 0 def
  69.      /rm 0 def /rmb 0 def
  70.  
  71.      % Find left and right boundaries of image
  72.      tm 1 bm
  73.       { % Get more data
  74.     arraydevice exch row copyscanlines pop
  75.     % Scan from left to find first non-zero element
  76.     % We save first the element, then the index
  77.     -1 0 1 loopcount
  78.     { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  79.     } for
  80.     % If we found -1, row is blank ..
  81.     dup -1 ne 
  82.     { % Find the leftmost index
  83.           dup lm lt
  84.           % If the new index is less, we save index and element
  85.           { /lm exch def /lmb exch def }
  86.           % If the index is equal, we or the bits together
  87.           { lm eq { lmb or /lmb exch def }{ pop } ifelse
  88.           } ifelse
  89.       % Now find the rightmost index
  90.       loopcount -1 0
  91.           { dup row exch get dup 0 ne { exch exit }{ pop pop } ifelse
  92.           } for
  93.       dup rm gt
  94.           % If the new index is greater, we save index and element
  95.           { /rm exch def /rmb exch def }
  96.           % If the index is equal, or the bits
  97.           { rm eq { rmb or /rmb exch def } { pop } ifelse
  98.           } ifelse
  99.     } if
  100.     pop
  101.       } for
  102.  
  103.      % Now we find the real left & right bit positions
  104.      256 0 1 7
  105.      { exch 2 div dup lmb le { pop exit }{ exch pop } ifelse
  106.      } for
  107.      /lmb exch def
  108.  
  109.      1 7 -1 0
  110.      { exch dup dup rmb and eq { pop exit }{ 2 mul exch pop } ifelse
  111.      } for
  112.      /rmb exch def
  113.  
  114.      % Calculate the bounding box values
  115.      /llx lm 8 mul lmb add def
  116.      /lly devheight bm sub def
  117.      /urx rm 8 mul rmb add def
  118.      /ury devheight tm sub def
  119.  
  120.      % Write out the magic string and bounding box information
  121.      epsifile (%!PS-Adobe-2.0 EPSF-1.2\n) writestring
  122.      epsifile (%%BoundingBox: ) writestring
  123.      epsifile llx write==only epsifile ( ) writestring
  124.      epsifile lly write==only epsifile ( ) writestring
  125.      epsifile urx write==only epsifile ( ) writestring
  126.      epsifile ury write==
  127.      epsifile (%%BeginPreview: ) writestring
  128.      epsifile urx llx sub 1 add write==only epsifile ( ) writestring
  129.      epsifile bm tm sub 1 add write==only epsifile ( 1 ) writestring
  130.      epsifile bm tm sub 1 add write==
  131.  
  132.      % Define character and bit widths for the output line buffer
  133.      /cwidth rm lm sub 1 add 8 mul 7 add 8 idiv def
  134.      /bwidth cwidth 8 mul def
  135.      /owidth urx llx sub 1 add 7 add 8 idiv def
  136.      /out cwidth string def
  137.  
  138.      % Create a 1-bit-high device for bitblt to align with the bbox
  139.      gsave
  140.      matrix cwidth 8 mul 1 <00 ff> makeimagedevice setdevice
  141.  
  142.      % 'image' a zero string to clear the line device
  143.      bwidth 1 1 matrix cwidth string image
  144.  
  145.      tm 1 bm
  146.       { % Get a scan line interval from the array device
  147.     arraydevice exch row copyscanlines lm cwidth getinterval
  148.     lmb 0 gt
  149.     { % 'image' it into the line device with the lmb offset
  150.       bwidth 1 1 [1 0 0 1 lmb 0] 5 -1 roll image
  151.       % Now we get the modified scan line
  152.       currentdevice 0 out copyscanlines 0 owidth getinterval
  153.     } if
  154.     % Write out the hex data
  155.     epsifile (% ) writestring 
  156.     epsifile exch writehexstring
  157.     epsifile (\n) writestring
  158.       } for
  159.  
  160.      epsifile (%%EndImage\n) writestring
  161.      epsifile (%%EndPreview\n) writestring
  162.      epsifile flushfile
  163.      grestore
  164.      erasepage initgraphics
  165.  
  166.      DonePage 0 1 put
  167.    } bind def
  168.  
  169.  
  170. (outfile) getenv
  171.   { /outfile exch def 
  172.     ps2epsi
  173.  
  174.     /DonePage 1 string def
  175.     (%stdin) (r) file cvx execute0
  176.     DonePage 0 get 0 eq { showpage } if
  177.   } if
  178.  
  179. end
  180. quit
  181.